<?php
/**
 * Created By PhpStorm
 * Author: patrick wu
 * Date: 2020/10/28
 * Time: 15:14
 */

use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
use SwooleTW\Http\Websocket\Facades\Websocket;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Redis;


/*
|--------------------------------------------------------------------------
| Websocket Routes
|--------------------------------------------------------------------------
|
| Here is where you can register websocket events for your application.
|
*/

Websocket::on('connect', function ($websocket, Request $request) {

    // called while socket on connect
    $source =   $request->query->get('source');
    $token  =   $request->query->get('Token', '');
    $request->headers->set('Authorization', "Bearer " . $token);

    /**
     * define the socket source
     *
     * call Controller@method
     */
    if ($source == 'backend') {
        if (empty($user_model =   Auth::guard('admin')->user())) {
            $websocket->emit('backendLogin', 'please login first');
            return false;
        }

        //set websocket sender and loginUsing
        $websocket->loginUsing($user_model);

        return App::call('App\Http\Controllers\Backend\ConnectController@onConnect', [
            'websocket' =>  $websocket,
            'request'   =>  $request,
        ]);
    } else {
        /**
         * check user login
         */
        if (empty($user = Auth::guard('api')->user())) {
            $websocket->emit('clientLogin', 'user not login, please login first!!!');
//            $websocket->close();
            return false;
        }

        $user_id   =   $user->uid ?? 0;

        /**
         * check user id online
         */
        /*if ($websocket->isUserIdOnline($user_id)) {
            $websocket->emit('clientWelcome', ['type' => 5, 'message' => '用户已登录']);     //->toUserId($user_id)
            return false;
        }*/

        //then bind user id to websocket
        $websocket->loginUsingId((int)$user_id);
        echo $user_id . ' is connecting...' . PHP_EOL;

        return App::call('App\Http\Controllers\Live\ConnectController@onConnect', [
            'websocket' =>  $websocket,
            'request'   =>  $request,
        ]);
    }
});

Websocket::on('disconnect', 'App\Http\Controllers\Live\ConnectController@onDisconnect');

/**
 * frontend listener event
 *
 * full namespace path
 */

/**
 * client ping event
 */
Websocket::on('clientPing', function ($websocket, $data) {
    $websocket->emit('clientPong', 'keep in touch.....');
});

/**
 * client emit this event after user stay this room for 10 seconds
 */
Websocket::on('clientInit', 'App\Http\Controllers\Live\ConnectController@onInit');                  //init data
Websocket::on('clientWatch', 'App\Http\Controllers\Live\RoomController@onWatch');                   //on watch

Websocket::on('clientFocus', 'App\Http\Controllers\Live\RoomController@onFocus');                   //on focus
Websocket::on('clientCancelFocus', 'App\Http\Controllers\Live\RoomController@onCancelFocus');       //on cancel focus
Websocket::on('clientThumbUp', 'App\Http\Controllers\Live\TalkController@onThumbUp');               //on thumb up
Websocket::on('clientShare', 'App\Http\Controllers\Live\TalkController@onShare');                   //client fetch user
Websocket::on('clientTalk', 'App\Http\Controllers\Live\TalkController@onTalk');                     //on talk
Websocket::on('clientClickGoods', 'App\Http\Controllers\Live\TalkController@onClickGoods');         //onClickGoods
Websocket::on('clientFetchCoupon', 'App\Http\Controllers\Live\TalkController@onFetchCoupon');        //client fetch user


/**
 * frontend emit event
 *
 * clientLogin                  ask user login first
 * clientWelcome                welcome new guy
 * clientLeave                  some guy leave
 * clientUsers                  room people number
 * clientTotalThumb             room total thumb
 * clientGoods                  goods change
 * clientRecommend              goods recommend
 * clientCoupons                coupons change
 * clientFetchResult            coupons fetch result
 * clientBlock                  user is blocked
 * clientUnblock                user is unblocked
 * clientReply                  anchor reply
 * clientPrivate                anchor private talk
 * clientReceiveMessage         client get message from others
 */

/**
 * backend listener event
 *
 * full namespace path
 */
Websocket::on('backendAddGoods', 'App\Http\Controllers\Backend\GoodsController@addGoods');         //add goods
Websocket::on('backendDeleteGoods', 'App\Http\Controllers\Backend\GoodsController@delGoods');      //delete goods
Websocket::on('backendUpdateGoods', 'App\Http\Controllers\Backend\GoodsController@upGoods');        //update goods
Websocket::on('backendAddCoupons', 'App\Http\Controllers\Backend\GoodsController@addCoupons');      //add coupons
Websocket::on('backendDeleteCoupons', 'App\Http\Controllers\Backend\GoodsController@delCoupons');   //delete coupons
Websocket::on('backendUpdateCoupons', 'App\Http\Controllers\Backend\GoodsController@upCoupons');     //update coupons
Websocket::on('backendRecommendGoods', 'App\Http\Controllers\Backend\GoodsController@setRecommend');  //recommend goods
Websocket::on('backendCancelRecommend', 'App\Http\Controllers\Backend\GoodsController@cancelRecommend');  //cancel recommend goods
Websocket::on('backendPriceGoods', 'App\Http\Controllers\Backend\GoodsController@priceGoods');      //price goods
Websocket::on('backendMoveGoods', 'App\Http\Controllers\Backend\GoodsController@moveGoods');        //move goods

Websocket::on('backendBlockUser', 'App\Http\Controllers\Backend\ReplyController@anchorBlock');        //user blocked
Websocket::on('backendUnblockUser', 'App\Http\Controllers\Backend\ReplyController@anchorUnblock');     //user unblocked
Websocket::on('backendAnchorSpeak', 'App\Http\Controllers\Backend\ReplyController@anchorSpeak');      //anchor speak
Websocket::on('backendNormalSpeak', 'App\Http\Controllers\Backend\ReplyController@normalSpeak');      //normal speak

Websocket::on('backendDataWatch', 'App\Http\Controllers\Backend\CountController@getAllData');      //getAllData
